package org.wyn.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

public class BBSUtil {
	private static final String INSER_STRING_SRC = "wjyuiansmile";
	private static final String INSER_STRING = CodeMD5.MD5_Code(INSER_STRING_SRC);
	private static final int[] POSITIONS = new int[]{2,5,6,8,9,10,14,17,18,22};

	public static String packString(String word) {
		String result = "";
		for (int i = 0; i <= POSITIONS.length ; i++) {
			int begin = i > 0 ? POSITIONS[i-1] : 0;
			int end = i < POSITIONS.length ? POSITIONS[i] : word.length();
			if(end > word.length()){
				end = word.length();
				result += word.substring(begin, end);
				return result;
			}else{
				result += word.substring(begin, end) + String.valueOf(INSER_STRING.charAt(i));
				
			}
		}
		return result;
	}
	
	public static String unPackString(String word) {
		StringBuilder sb = new StringBuilder(word);
		for (int i = 0; i <= POSITIONS.length ; i++) {
			int index = i < POSITIONS.length ? POSITIONS[i] : sb.length()-1;
			if(index > sb.length()){
				return sb.toString();
			}else{
				sb.deleteCharAt(index);
			}
		}
		return sb.toString();
	}

	public static <K, V extends Serializable> List<V> mapToList(Map<K, V> map) {
		List<V> result = new ArrayList<V>(0);
		try {
			Set<K> keySets = map.keySet();
			Iterator<K> it = keySets.iterator();
			while(it.hasNext()){
				result.add(map.get(it.next()));
			}
			return result;
		} catch (Exception e) {
			return result;
		}
	}
	
	public static String HtmltoText(String inputString) {
        String htmlStr = inputString; //含html标签的字符串
        String textStr ="";
        java.util.regex.Pattern p_;
        java.util.regex.Matcher m_;
        java.util.regex.Pattern p_style;
        java.util.regex.Matcher m_style;
        java.util.regex.Pattern p_html;
        java.util.regex.Matcher m_html;
        java.util.regex.Pattern p_ba;
        java.util.regex.Matcher m_ba;
        if(inputString == null || "".equalsIgnoreCase(inputString)){
        	return "";
        }
        try {
            String regEx_ = "<[\\s]*?[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?[\\s]*?>"; //定义的正则表达式{或<[^>]*?>[\\s\\S]*?<\\/> }
            String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; //定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style> }
            String regEx_html = "<[^>]+>"; //定义HTML标签的正则表达式
            String patternStr = "\\s+";

            p_ = Pattern.compile(regEx_,Pattern.CASE_INSENSITIVE);
            m_ = p_.matcher(htmlStr);
            htmlStr = m_.replaceAll(""); //过滤标签

            p_style = Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
            m_style = p_style.matcher(htmlStr);
            htmlStr = m_style.replaceAll(""); //过滤style标签

            p_html = Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
            m_html = p_html.matcher(htmlStr);
            htmlStr = m_html.replaceAll(""); //过滤html标签

            p_ba = Pattern.compile(patternStr,Pattern.CASE_INSENSITIVE);
            m_ba = p_ba.matcher(htmlStr);
            htmlStr = m_ba.replaceAll(""); //过滤空格

         textStr = htmlStr;

        }catch(Exception e) {
                    System.err.println("Html2Text: " + e.getMessage());
        }
        return textStr;//返回文本字符串
     }

	/**
	 * 截取字符串
	 * @param src 目标字符串
	 * @param l 半角长度
	 * @return
	 */
	public static String cut(String src, int l, String ending){
		if(src.getBytes().length > l){
			StringBuilder sb = new StringBuilder("");
			int clength = 0;
			int i = 0;
			int len = src.length();
			while(clength < l && i < len){
				String chStr = String.valueOf(src.charAt(i));
				sb.append(chStr);
				clength += chStr.getBytes().length;
				i ++;
			}
			if(clength > l){
				sb.deleteCharAt(sb.length() - 1);
			}
			return sb.toString() + ending;
		}else{
			return src;
		}
	} 
	public static String cut(String src, int l){
		return cut(src, l, "");
	}
	
	public static void main(String[] args) {
		String p = "9999sdrfwer4werfd424";
		String pp = packString(p);
		System.out.println(pp);
		System.out.println(unPackString(pp));
		System.out.println(p);
	}
}
